#include <iostream>
#include <string>
using namespace std;

class Solution
{
public:
    string longestPalindrome(string s)
    {
        int maxRange = 0;
        int maxstart = 0;
        int n = s.length();
        for (int i = 0; i < n; i++)
        {
            // check even
            int step = 0;
            while (i - step > -1 && i + step < n && s[i - step] == s[i + step])
                step++;
            if (maxRange < step * 2 - 1)
            {
                maxRange = step * 2 - 1;
                maxstart = i;
            }
            // check odd
            step = 0;
            while (i - step > -1 && i + step + 1 < n && s[i - step] == s[i + step + 1])
                step++;
            if (maxRange < step * 2)
            {
                maxRange = step * 2;
                maxstart = i;
            }
        }
        return s.substr(maxstart - (maxRange - 1) / 2, maxRange);
    }
};

int main()
{
    string str = "ababc";
    Solution s;
    cout << s.longestPalindrome("ababc") << endl;
    cout << s.longestPalindrome("dccb") << endl;
    cout << s.longestPalindrome("bb") << endl;
    return 0;
}